//
//  EditCardDetailsController.m
//  DiscountClub
//
//  Created by Philip Chew on 28/2/12.
//  Copyright (c) 2012 articwind@gmail.com. All rights reserved.
//

#import "EditCardDetailsController.h"

@implementation EditCardDetailsController
@synthesize txtName, txtCardNo, txtCompany;

- (id) initWithEditCardDetailState:(id<EditCardDetailState> )state {
	if(([self initWithNibName:@"EditCardDetailsController" bundle:nil])) {
		uiState = state;
		
		companies = [[NSMutableArray alloc] init];
		[companies addObject:@"Test A"];
		[companies addObject:@"Test B"];
		[companies addObject:@"Test C"];
		[companies addObject:@"Test D"];
	}
	
	return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
	
    // Do any additional setup after loading the view from its nib.
	self.title = uiState.title;
	[uiState setControlStateForName:txtName];
	[uiState setControlStateForCardNo:txtCardNo];
	[uiState setControlStateForCompany:txtCompany];
	[uiState setControlStateForFrontImage:btnCardFront];
	[uiState setControlStateForBackImage:btnCardBack];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - UI Events
- (IBAction)onBeginEditing_txtCompany
{
	[txtCompany resignFirstResponder];
	UIPickerView *pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 394, 320, 270)];
    pickerView.delegate = self;
    pickerView.dataSource = self;
    [self.view addSubview:pickerView];
	
	[UIView beginAnimations:@"pickerView" context:nil];
    [UIView setAnimationDuration:0.5];
    pickerView.transform = CGAffineTransformMakeTranslation(0, -150);
    [UIView commitAnimations];
	
    [pickerView release];
}

- (IBAction)onClick_cardFront
{
	selectedButton = btnCardFront;
	[self displayPictureOptions];
}

- (IBAction)onClick_cardBack
{
	selectedButton = btnCardFront;
	[self displayPictureOptions];	
}

- (IBAction)onClick_save
{
	
}

#pragma mark - UI Picker View elements
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
	return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
	return [companies count];
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
	return [companies objectAtIndex:row];
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
	txtCompany.text = [companies objectAtIndex:row];

	[UIView beginAnimations:@"picker" context:nil];
    [UIView setAnimationDuration:0.5];
    pickerView.transform = CGAffineTransformMakeTranslation(0,236);
    [UIView commitAnimations];
}

#pragma mark - UI Image Picker View Elements
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo
{
	[picker dismissModalViewControllerAnimated:YES];
}

#pragma mark - UI Action Sheet Elements
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex 
{
	UIImagePickerController *imgPicker = [[UIImagePickerController alloc] init];
	imgPicker.delegate = self;
	imgPicker.allowsEditing = YES;
	imgPicker.sourceType = buttonIndex == 0 ? UIImagePickerControllerSourceTypePhotoLibrary : UIImagePickerControllerSourceTypeCamera;
	[self presentModalViewController:imgPicker animated:YES];
	[imgPicker release];
}

#pragma mark - Private Events
- (void) displayPictureOptions
{
	UIActionSheet *uiActionSheet = [[UIActionSheet alloc] initWithTitle:@"Select Image Source" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"Select From Phone", @"Take Picture", nil];
	[uiActionSheet showInView:self.view];
	[uiActionSheet release];
}
@end
